package main

import (
	"bytes"
	_ "embed"
	"strings"
	"text/template"
)

//go:embed ginTemplate.tpl
var ginTemplate string

type serviceDesc struct {
	ServiceType string
	ServiceName string
	Methods     []*methodDesc
	MethodMap   map[string]*methodDesc
}

type ParamType int

const (
	ParamUri   = 0
	ParamQuery = 1
	ParamJson  = 2
)

// method
type methodDesc struct {
	Name         string
	Request      string
	Reply        string
	Comment      string
	Path         string
	Method       string
	ParamType    ParamType
	RequestBody  string
	ResponseBody string
}

func (s *serviceDesc) execute() string {
	s.MethodMap = make(map[string]*methodDesc)
	for _, m := range s.Methods {
		s.MethodMap[m.Name] = m
	}
	buf := new(bytes.Buffer)
	tmpl, err := template.New("gin").
		Parse(strings.TrimSpace(ginTemplate))
	if err != nil {
		panic(err)
	}
	if err := tmpl.Execute(buf, s); err != nil {
		panic(err)
	}
	return strings.Trim(buf.String(), "\r\n")
}
